package com.linln.modules.system.service.impl;

import com.linln.common.data.QuerySpec;
import com.linln.common.utils.DateUtils;
import com.linln.modules.system.domain.Order;
import com.linln.modules.system.domain.OrderAmounts;
import com.linln.modules.system.repository.OrderAmountsRepository;
import com.linln.modules.system.service.OrderAmountsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

/**
 * @author 通义灵码
 * @date 2023/10/10
 */
@Service
public class OrderAmountsServiceImpl implements OrderAmountsService {

    @Autowired
    private OrderAmountsRepository orderAmountsRepository;

    @Override
    public void updateOrderAmounts(Order order,String msg,int type) {
        Double price = order.getPrice();
        Double balance = 0.00;
        if (null!=order.getCurrentMoney()){
            balance = order.getCurrentMoney();
        }
        //判断是陪护还是陪诊陪检
        int type1 = order.getType();
        int stageType = 0;
        int hourNum = 0;
        if (type1==1){
            stageType = order.getStageType();
            if (stageType==2){
                hourNum = order.getHourNum();
            }
        }
        //消费一次金额变动
        OrderAmounts orderAmounts2 = new OrderAmounts();

        QuerySpec querySpec = QuerySpec.matching();
        querySpec.withMatcherValue("orderId", QuerySpec.EQUAL,String.valueOf(order.getId()));
        querySpec.withMatcherValue("status", QuerySpec.EQUAL,"1");

        List<OrderAmounts> amountsList = orderAmountsRepository.findAll(QuerySpec.of(querySpec), Sort.by(Sort.Direction.DESC,"id"));

        String createDay = DateUtils.getDate();
        //判断是否需要更新记录
        int flag = 1;
        if (amountsList.size()>0) {
            OrderAmounts orderAmounts1 = amountsList.get(0);
            Double suffixred = orderAmounts1.getSuffixred();
            orderAmounts2.setPreinc(suffixred);
            //判断变动类型1消费2预存3退款
            if (type==1) {
                orderAmounts2.setIncrease((double) 0);
                if (type1==1){
                    //1按阶段选择 2按小时选择
                    if (stageType==1){
                        orderAmounts2.setReduce(price);
                        BigDecimal tempPrice = new BigDecimal(suffixred).subtract(new BigDecimal(price));
                        BigDecimal result = tempPrice.setScale(2, RoundingMode.HALF_UP);
                        orderAmounts2.setSuffixred(result.doubleValue());
                    }else{
                        BigDecimal dprice = new BigDecimal(price);
                        BigDecimal dsuf = new BigDecimal(suffixred);
                        BigDecimal mul = dprice.multiply(new BigDecimal(hourNum));

                        BigDecimal result = dsuf.subtract(mul);
                        BigDecimal results = result.setScale(2, RoundingMode.HALF_UP);


                        orderAmounts2.setReduce(mul.doubleValue());
                        orderAmounts2.setSuffixred(results.doubleValue());
                    }
                }else{
                    orderAmounts2.setReduce(price);
                    BigDecimal tempPrice = new BigDecimal(suffixred).subtract(new BigDecimal(price));
                    BigDecimal result = tempPrice.setScale(2, RoundingMode.HALF_UP);
                    orderAmounts2.setSuffixred(result.doubleValue());
                }

            } else if (type == 2) {
                orderAmounts2.setReduce((double) 0);
                orderAmounts2.setIncrease(balance);

                BigDecimal dprice = new BigDecimal(suffixred);
                BigDecimal dsuf = new BigDecimal(balance);
                BigDecimal res = dprice.add(dsuf);
                BigDecimal results = res.setScale(2, RoundingMode.HALF_UP);
                double tempPrice = results.doubleValue();
                orderAmounts2.setSuffixred(tempPrice);
            } else if (type == 3) {
                orderAmounts2.setIncrease((double) 0);
                orderAmounts2.setReduce(suffixred);
                orderAmounts2.setSuffixred((double) 0);
                if (suffixred<=0){
                    flag = 0;
                }
            }
        }else{
            //判断变动类型1消费2预存3退款
            if (type==1) {
                orderAmounts2.setPreinc(balance);
                orderAmounts2.setIncrease((double) 0);
                if (type1==1){
                    //1按阶段选择 2按小时选择
                    if (stageType==1){
                        orderAmounts2.setReduce(price);
                        BigDecimal tempPrice = new BigDecimal(balance).subtract(new BigDecimal(price));
                        BigDecimal result = tempPrice.setScale(2, RoundingMode.HALF_UP);
                        orderAmounts2.setSuffixred(result.doubleValue());
                    }else{
                        BigDecimal dprice = new BigDecimal(price);
                        BigDecimal mul = dprice.multiply(new BigDecimal(hourNum));

                        BigDecimal dsuf = new BigDecimal(balance);
                        BigDecimal result = dsuf.subtract(mul);
                        BigDecimal results = result.setScale(2, RoundingMode.HALF_UP);

                        orderAmounts2.setReduce(mul.doubleValue());
                        orderAmounts2.setSuffixred(results.doubleValue());
                    }
                }else{
                    orderAmounts2.setReduce(price);
                    BigDecimal tempPrice = new BigDecimal(balance).subtract(new BigDecimal(price));
                    BigDecimal result = tempPrice.setScale(2, RoundingMode.HALF_UP);
                    orderAmounts2.setSuffixred(result.doubleValue());
                }
            } else if (type == 2) {
                orderAmounts2.setPreinc((double) 0);
                orderAmounts2.setReduce((double) 0);
                orderAmounts2.setIncrease(balance);
                orderAmounts2.setSuffixred(balance);
            }
        }
        if (flag==1){
            orderAmounts2.setRemarks(msg);
            orderAmounts2.setOrderId(order.getId());
            orderAmounts2.setPatientId(order.getPatientId());
            orderAmounts2.setPatientAdmissionId(order.getPatientAdmissionId());
            orderAmounts2.setType((byte) type);
            orderAmounts2.setStatus((byte) 1);
            orderAmounts2.setCreateDay(createDay);
            //余额变动表
            orderAmountsRepository.save(orderAmounts2);
        }
    }
}
